using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

using LibrErian.Common;
using LibrErian.Model;
using LibrErian.Interfaces.Services;
using Microsoft.Practices.CompositeUI;

namespace LibrErian.Modules.BasicBook
{
    public class SelectAuthorViewPresenter : Presenter<ISelectAuthorView>
    {
        private IAuthorService authorService;

        [ServiceDependency]
        public IAuthorService AuthorService
        {
            set
            {
                authorService = value;
            }
        }

        private Author AddNewAuthor()
        {
            ControlledWorkItem<NewAuthorController> workItem = WorkItem.WorkItems.AddNew<ControlledWorkItem<NewAuthorController>>();
            return workItem.Controller.Run(true);
        }

        public void OnCancel()
        {
            View.SelectedAuthor = null;
            CloseView();
        }

        public void OnOk()
        {
            CloseView();
        }

        public void OnNew()
        {
            View.SelectedAuthor = AddNewAuthor();
            CloseView();
        }

        public override void OnViewReady()
        {
            base.OnViewReady();

            View.SetData(authorService.GetAuthors());
        }
    }

    public interface ISelectAuthorView
    {
        Author SelectedAuthor
        {
            get;
            set;
        }

        void SetData(IList authors);
    }
}
